﻿using System;
using System.Collections.Generic;
using System.Text;

using MetaHeuristics.Collections;

namespace MetaHeuristics.Aco.Collections
{
    public sealed class TourCollection : ICollection<int>
        ,System.Collections.ICollection
        ,System.Collections.IEnumerable
    {
        private List<int> cities = new List<int>();
        private HashSet<int> dic = new HashSet<int>();
        private float length = 0;

        public TourCollection()
        {}

        public TourCollection(TourCollection tours)
        {
            this.length = tours.length;
            this.cities = new List<int>(tours.Count);
            for (int i = 0; i < tours.cities.Count; ++i)
            {
                var city = tours.cities[i];
                this.cities.Add(city);
                this.dic.Add(city);
            }
        }

        public void Add(int city)
        {
            cities.Add(city);
            dic.Add(city);
        }

        public void Close()
        {
            cities.Add(cities[0]);
        }

        public void Clear()
        {
            this.cities.Clear();
            this.dic.Clear();
        }

        public void Swap(int i, int j)
        {
            int temp = this.cities[i];
            this.cities[i] = this.cities[j];
            this.cities[j] = temp;
        }

        public float Length
        {
            get
            {
                return this.length;
            }
            set
            {
                this.length = value;
            }
        }

        public int this[int index]
        {
            get
            {
                return cities[index];
            }
            set
            {
                cities[index] = value;
            }
        }

        public int Count
        {
            get
            {
                return this.cities.Count;
            }
        }

        public bool Contains(int city)
        {
            return dic.Contains(city);
        }

        public IEnumerator<int> GetEnumerator()
        {
            return this.cities.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.cities.GetEnumerator();
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public void CopyTo(int[] array, int index)
        {
            this.cities.CopyTo(array, index);
        }

        void System.Collections.ICollection.CopyTo(Array array, int index)
        {
            this.CopyTo((int[])array, index);
        }

        public bool Remove(int city)
        {
            throw new NotImplementedException();
        }

        public Object SyncRoot
        {
            get
            {
                return null;
            }
        }

        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }
    }
}
